/*
 * Anything: A flexible and easy to use data structure that combines the 
 * characteristics of a list and a hashmap into one data structure.
 * 
 * Based on a paper by Peter Sommerlad and Marcel Rueedi on 
 * 'Do-it-yourself reflection' entered at EuroPLOP 1998.
 * 
 * This library was written completely from scratch and is based only 
 * in spirit on the original Java Anything implementation written by 
 * Andre Weinand/Marcel Rueedi in 1997/99 and enhanced by Stefan Tramm 
 * in 2006.
 * 
 * Copyright (c) 2008 Thomas Marti and others. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package any.functor.impl.func;

import java.util.Map.Entry;

import any.ImmutableAny;
import any.functor.Predicates;
import any.functor.interfaces.Predicate;
import any.util.Direction;

/**
 * 
 *
 * @param <TO> Type of the function output
 * @param <DS> Type for default/start value
 * @param <T>  Implementation type of the function
 */
public abstract class BaseAnyFunction<TO, DS, T extends BaseAnyFunction<TO, DS, T>> {

	protected Predicate<? super Entry<String, ? extends ImmutableAny>> predicate;

	protected Direction direction;
	
	protected DS startValue;
	
	protected DS defaultValue;

	protected boolean hasDefault;

	protected boolean hasStartValue;

	protected Class<TO> type;

	protected int fromIndex = -1;

	protected int toIndex = -1;
	
	protected boolean exact = false;
	
	protected String description;

	public BaseAnyFunction() {
	}
	
	public BaseAnyFunction(String description) {
		this(null, description);
	}
	
	public BaseAnyFunction(Class<TO> type) {
		this(type, null);
	}
	
	public BaseAnyFunction(Class<TO> type, String description) {
		this.type = type;
		this.description = description;
	}
	
	@SuppressWarnings("unchecked")
	public T order(Direction direction) {
		this.direction = direction;
		return (T)this;
	}
	
	public T reverse() {
		return order(Direction.BACKWARD);
	}
	
	@SuppressWarnings("unchecked")
	public T select(Predicate<? super Entry<String, ? extends ImmutableAny>> predicate) {
		this.predicate = predicate;
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T filter(Predicate<? super Entry<String, ? extends ImmutableAny>> predicate) {
		if (predicate != null) {
			this.predicate = Predicates.not(predicate);
		} else {
			this.predicate = null;			
		}
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T withDefault(DS defaultValue) {
		this.hasDefault = true;
		this.defaultValue = defaultValue;
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T startValue(DS startValue) {
		this.hasStartValue = true;
		this.startValue = startValue;
		return (T)this;
	}
	
	public Class<TO> getTargetType() {
		return type;
	}

	public T sublist(int fromIndex) throws IllegalArgumentException {
		return sublist(fromIndex, Integer.MAX_VALUE);
	}

	@SuppressWarnings("unchecked")
	public T sublist(int fromIndex, int toIndex) throws IllegalArgumentException {
		if (fromIndex < 0 || toIndex < fromIndex) throw new IndexOutOfBoundsException("Invalid indices from: " + fromIndex + " / to: " + toIndex);
		this.fromIndex = fromIndex;
		this.toIndex = toIndex;
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T exact() {
		exact = true;
		return (T)this;
	}
	
}
